<?php
/**
 * User: 魔十七
 * Date: 2020/11/5
 * Time: 13:44
 */

namespace Admin\Controller;
class AdsController extends JudgeLoginController
{
    //列表
    public function index(){
         $this->display();
    }
    //列表数据
    public function getList(){
        $ads = M('ads')->order('id desc')->page($_GET['page'].',20')->select();
        $count = M('ads')->order('id desc')->count();
        $data = ['code'=>'0','msg'=>'','count'=>$count,'data'=>$ads];
        echo json_encode($data);
    }

    //添加
    public function create(){
        $this->display();
    }

    public function save(){
        $go_back['code']=1001;
        $post=$_POST;
        $id=isset($post['id']) ? $post['id'] : '';
        if($id){
            unset($post['id']);
            $ads = M('ads')->where("id=$id")->save($post);
        }else{
            $ads = M('ads')->add($post);
        }

        if(!$ads){
            $go_back['code']=1002;
        }
        $this->ajaxReturn($go_back);
    }
    //删除
    public function delete(){
        $go_back['code']=1001;
        $post=$_POST;
        $id=isset($post['id']) ? $post['id'] :$this->ajaxReturn(['code'=>1002]);
        M('ads')->delete($id);
        $this->ajaxReturn($go_back);
    }

    public function edit($id){
        $data=M('ads')->where("id=$id")->find();
        if($data){
            $this->assign('data',$data);
            $this->display();
        }else{
            $this->error('异常');
        }
    }
}